home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include "exp_hdr.h"
- #include "rex_hdr.h"
- #include "tmenu_ex.h"
- #include "hook.h"
-
- #define TRUE (1)
- #define FALSE (0)
- #define VARLEN 12
-
- char fname[256], line[80], var[16];
- char digit[] = "0123456789ABCDEF";
- struct rex_head hdr;
-
- int link_head;
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- FILE *fp;
- struct exp_head hdr;
- struct node link;
- long cur_node;
- struct rex_head rhdr;
- char *typestr, *valuestr;
- int i, found, offset, base, type, value, nth;
- extern char *strchr();
-
- if (argc != 5){
- fprintf(stderr, "Usage: setvar EXG-file var type value\n");
- exit(-1);
- }
- if (argc == 5){
- strncpy(fname, argv[1], sizeof fname - 5);
- strcat(fname, ".map");
- strncpy(var, argv[2], VARLEN);
- typestr = argv[3];
- valuestr = argv[4];
- } else {
- strncpy(fname, argv[4], sizeof fname);
- strncpy(var, argv[5], VARLEN);
- typestr = argv[6];
- valuestr = argv[7];
- nth = atoi(argv[3]);
- }
- for (i = strlen(var); i < sizeof var; i++)
- var[i] = ' ';
- if (strcmp(typestr, "a") == 0) /* string */
- type = 0;
- else if (strcmp(typestr, "b") == 0) /* 1 byte */
- type = 1;
- else if (strcmp(typestr, "w") == 0) /* 2 byte */
- type = 2;
- else if (strcmp(typestr, "d") == 0) /* 4 byte */
- type = 4;
- else {
- fprintf(stderr, "Error: invalid type %s\n", typestr);
- exit(1);
- }
-
- /* get offset of var */
- if ((fp = fopen(fname, "r")) == NULL){ /* open .MAP file */
- perror(fname);
- exit(-1);
- }
- found = FALSE;
- while (fgets(line, sizeof line, fp) != NULL){
- if (strncmp(line, "Public symbols", 14) == 0){
- found = TRUE;
- break;
- }
- }
- if (!found){
- fprintf(stderr, "Error: Public symbols not found.\n");
- exit(-1);
- }
- fgets(line, sizeof line, fp);
- fgets(line, sizeof line, fp);
- fgets(line, sizeof line, fp);
- found = FALSE;
- while (fgets(line, sizeof line, fp) != NULL){
- if (strncmp(line, var, VARLEN) == 0){
- found = TRUE;
- break;
- }
- }
- fclose(fp);
- if (!found){
- var[VARLEN] = '\0';
- fprintf(stderr, "Error: %s not found.\n", var);
- exit(-1);
- }
- offset = 0;
- for (i = 14; i < 22; i++){
- offset *= 16;
- offset += strchr(digit, line[i]) - digit;
- }
-
- if (argc == 5){
- strncpy(fname, argv[1], sizeof fname - 5);
- strcat(fname, ".exg");
- if ((fp = fopen(fname, "rb")) == NULL){
- perror(fname);
- exit(-1);
- }
- fread(&rhdr, REX_HEADER_SIZE, 1, fp);
- if (!(rhdr.magic[0] == 'M' && rhdr.magic[1] == 'Q'
- && rhdr.rel_off == 0x1e && rhdr.overlay == 0
- && rhdr.one == 1)){
- fprintf(stderr, "Error: Can't handle this REX-header\n");
- exit(1);
- }
- offset = (rhdr.header_para << 4) + offset;
- } else {
- strncpy(fname, argv[1], sizeof fname);
- if ((fp = fopen(fname, "rb")) == NULL){
- perror(argv[1]);
- exit(-1);
- }
- fread(&hdr, sizeof hdr, 1, fp);
- if (!(hdr.magic[0] == 'P' && hdr.magic[1] == '3'
- && hdr.level == 1 && hdr.header_size == 0x0180
- && hdr.runhead_off == 0x0180 && hdr.runhead_size == 0x80
- && hdr.reloc_size == 0 && hdr.seginfo_size == 0
- && *(long *)hdr.image_off==0x0200 && *(long *)hdr.sym_size==0
- && *(long *)hdr.gdt_size == 0 && *(long *)hdr.ldt_size == 0
- && *(long *)hdr.idt_size == 0 && *(long *)hdr.tss_size == 0
- && *(long *)hdr.image_size == hdr.imagefile_size
- && *(long *)hdr.image_size + *(long *)hdr.min_alloc == *(long *)hdr.esp)){
- fprintf(stderr, "Error: Can't handle this header: %s\n", argv[1]);
- exit(1);
- }
-
- if ((link_head = pattern_search(fp, HEAD_ID, sizeof HEAD_ID)) <0){
- fprintf(stderr, "Error: Can't find %s\n", HEAD_ID);
- exit(1);
- }
- link_head -= (8 + *(long *)hdr.image_off);
-
- cur_node = link_head;
- base = cur_node + *(long *)hdr.image_off;
- fseek(fp, base, 0L);
- fread(&link, sizeof link, 1, fp);
- if (strncmp(link.id, HEAD_ID, 8) != 0){
- fprintf(stderr, "Error: version error\n");
- exit(1);
- }
- found = FALSE;
- if (strcmp(HEAD_ID, argv[2]) == 0){
- base -= 4;
- } else {
- while (link.next != 0 && !found){
- base = link.next + *(long *)hdr.image_off;
- fseek(fp, base, 0L);
- cur_node = link.next;
- fread(&link, sizeof link, 1, fp);
- if (strncmp(link.id, argv[2], strlen(argv[2]))==0 && --nth == 0)
- found = TRUE;
- }
- if (!found){
- fprintf(stderr, "Error: module %s not found.\n", argv[2]);
- exit(-1);
- }
- }
- offset = base + offset;
- }
- fclose(fp);
- printf("offset = 0x%x\n", offset);
-
- /* set var */
- if ((fp = fopen(fname, "rb+")) == NULL){
- perror(fname);
- exit(-1);
- }
- fseek(fp, offset, 0);
- if (type == 0){ /* string */
- fputs(valuestr, fp);
- fputc('\0', fp);
- } else { /* binary */
- value = atoi(valuestr);
- while (--type >= 0){
- fputc(value & 0xff, fp);
- value >>= 8;
- }
- }
- fclose(fp);
-
- exit(0);
- }
-